package com.example.jnet.services;

import com.example.jnet.pojo.MessageProtocol;
import com.example.jnet.tools.ToolUtils;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class RegisterService implements IMessageService {
    private static Logger logger = LoggerFactory.getLogger(RegisterService.class);

    @Override
    public Integer getHandlerType() {
        return MessageProtocol.PROTOCOL_REGISTER;
    }

    @Override
    public boolean doService(ChannelHandlerContext ctx, MessageProtocol msg) {
        logger.info("ctx: {}, msg:{}", ctx.channel().toString(), msg.toString());

        MessageProtocol response = new MessageProtocol();

        response.setType(ToolUtils.int2bytes(MessageProtocol.PROTOCOL_REGISTER, 4));
        String body = "ack info!!!";
        int length = body.getBytes().length + MessageProtocol.DEFAULT_HEADER_LENGTH;
        response.setLength(ToolUtils.int2bytes(length, 4));
        response.setBody(body.getBytes());

        logger.info("response:" + response.toString());
        ctx.channel().writeAndFlush(response.createByteBuf());
        return true;
    }
}
